package com.gouwudai.gobbs.dao.impl;

import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.gouwudai.gobbs.common.IPageList;
import com.gouwudai.gobbs.dao.IClassPermissionDAO;
import com.gouwudai.gobbs.pojo.TbbsClassPermission;
import com.litt.core.common.PageParam;
import com.litt.core.common.Utility;
import com.litt.core.exception.BusiException;
/**
 * 
 * <pre>
 *  Title:用户分类/版面权限DAO
 *  Description: 为用户设置某一分类/版面的特有权限 
 * </pre>
 * 
 * @author <a href="mailto:littcai@hotmail.com">空心大白菜</a>
 * @date 2007-3-29
 * @version 1.0
 * <pre>
 * 修改记录
 *    修改后版本:     修改人：  修改日期:     修改内容: 
 * </pre>
 */
public class ClassPermissionDAOImpl extends BaseDAOImpl implements IClassPermissionDAO 
{
	private static final Log logger = LogFactory.getLog(ClassPermissionDAOImpl.class);	
	private static final String BUSI_NAME = "分类/版面权限";	
	
	/**
	 * 保存用户分类/版面权限
     * @param tbbsClassPermission
	 * @throws BusiException
	 */
	public void save(TbbsClassPermission tbbsClassPermission)
	{             
		super.saveObject(tbbsClassPermission);		
	}
	
	/**
	 * 读取分类版面权限
     * @param id
	 * @return
	 */
	public TbbsClassPermission load(Integer id)
	{
		return (TbbsClassPermission)super.loadObject(TbbsClassPermission.class, id);
	} 
	
	/**
	 * 按登陆ID和版面ID查询对应权限
     * @param classId 分类版面ID
	 * @param loginId 登陆ID
	 * @return
	 */
    public List listByLoginId(Integer classId,String loginId)
    {
    	String listHql = "select permissionCode from TbbsClassPermission a TbbsUserInfo where a.type=0 and a.classId="+classId+" and a.userId=b.userId and b.loginId='"+loginId+"'";
    	return super.listAll(listHql);
    }
    
	/**
	 * 按用户ID和版面ID查询拥有的权限
     * @param classId 分类版面ID
	 * @param userId 用户ID
	 * @return
	 */
    public List listByUserId(Integer classId,Integer userId)
    {
    	String listHql = "select b from TbbsClassPermission a,TbbsPermission b where a.type=0 and a.classId="+classId+" and a.userId="+userId+" and a.permissionCode=b.permissionCode" ;
        return super.listAll(listHql); 
    } 
    
    /**
     * 按用户ID和版面ID查询未拥有的权限
     * @param classId 分类版面ID
     * @param userId 用户ID
     * @return
     */
    public List listNotByUserId(Integer classId,Integer userId)
    {
        String listHql = "select b from TbbsPermission b where b.permissionCode>3000 and b.permissionCode<5000 and b.permissionCode not in(select a.permissionCode from TbbsClassPermission a where a.type=0 and a.classId="+classId+" and a.userId="+userId+")" ;
        return super.listAll(listHql); 
    }     
    
	/**
	 * 按用户组ID和版面ID查询对应权限
     * @param classId
	 * @param gradeId
	 * @return
	 */
    public List listByGradeId(Integer classId,String gradeId)
    {
    	String listHql = "select permissionCode from TbbsClassPermission where type=1 and classId="+classId+" and gradeId="+gradeId;
        return super.listAll(listHql); 	
    }

    /**
	 * 删除权限
     * @param cpId
	 */
    public void delete(Integer cpId)
    {
        super.deleteObject(TbbsClassPermission.class.getName(),"cpId",cpId);
    }
    
    /**
     * 根据版面和用户删除权限.
     * 
     * @param classId 版面ID
     * @param userId 用户ID
     */
    public void deleteByClassUser(Integer classId,Integer userId)
    {
        String hql = "delete from TbbsClassPermission obj where obj.classId=? and obj.userId=?";
        super.updateObject(hql,new Object[]{classId,userId});
    }    
    
    /**
	 * 批量删除权限
     * @param cpIds
	 */
    public void deleteBatch(String cpIds)
    {
        super.deleteObjects(TbbsClassPermission.class.getName(),"cpId",cpIds);
    } 
    
    /**
     * 查询用户所有版面的所有权限
     * @param userId 用户ID
     * @return
     */
    public List listAllPermission(Integer userId)
    {
        String listHql = "select obj,b.permissionName from TbbsClassPermission obj,TbbsPermission b where obj.type=0 and obj.userId="+userId+" and obj.permissionCode=b.permissionCode";
        return super.listAll(listHql); 
    }
    
    /**
	 * @param pageIndex
	 * @param pageSize
	 * @param sort
	 * @return
	 */
    public IPageList listPage(int pageIndex,int pageSize,String sort)
    {       
    	StringBuilder listSql = new StringBuilder("SELECT A.*,B.GRADE_NAME AS UG_NAME,'等级' AS UG_TYPE_NAME,C.CLASS_NAME FROM TbbsClassPermission A ,TbbsGrade B,TbbsClass C WHERE A.TYPE=1 AND A.GRADE_ID=B.GRADE_ID AND A.CLASS_ID=C.CLASS_ID ");
    	listSql.append(" UNION ALL SELECT A.*,B.LOGIN_ID AS UG_NAME,'用户' AS UG_TYPE_NAME,C.CLASS_NAME FROM TbbsClassPermission A ,TbbsUserInfo B,TbbsClass C WHERE A.TYPE=0 AND A.USER_ID=B.USER_ID AND A.CLASS_ID=C.CLASS_ID ");
    	if(!Utility.isEmpty(sort))
    	    listSql.append(sort);
    	return super.listSqlPage(listSql.toString(),pageIndex,pageSize);
    }    
}
